package com.mlx.mbg.code.method;

import com.mlx.mbg.code.inter.EnableMethod;
import com.mlx.mbg.code.config.MbgPropertyConfig;
import com.mlx.mbg.code.inter.ExtensionLevel;
import com.mlx.mbg.code.inter.IExtensionMethod;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;

import java.util.Collections;
import java.util.List;

@EnableMethod
public class InsertBatchMethod implements IExtensionMethod {
    @Override
    public boolean checkConfig(ExtensionLevel level, MbgPropertyConfig mbgPropertyConfig, IntrospectedTable introspectedTable) {
        return getExtensionLevelSet().contains(level) && mbgPropertyConfig.getEnableInsertBatch();
    }

    @Override
    public int getOrder() {
        return 200;
    }

    @Override
    public List<Method> buildMethod(IntrospectedTable introspectedTable) {
        JavaVisibility methodVisibility = JavaVisibility.PUBLIC;

        // 设置返回类型是List
        FullyQualifiedJavaType methodReturnType = new FullyQualifiedJavaType("int");

        String methodName = "insertBatch";

        // 设置参数类型
        FullyQualifiedJavaType methodParameterType = new FullyQualifiedJavaType("Collection");
        //给参数类型补充泛型
        methodParameterType.addTypeArgument(new FullyQualifiedJavaType(introspectedTable.getBaseRecordType()));
        // 方法参数
        Parameter methodParameter = new Parameter(methodParameterType, "dataCollection");

        Method method = new Method();
        method.setVisibility(methodVisibility);
        method.setReturnType(methodReturnType);
        method.setName(methodName);
        method.addParameter(methodParameter);
        // 设置备注
        method.addJavaDocLine("/**");
        method.addJavaDocLine(" * 批量插入；数据集合为空会报错，使用时请判断是否空");
        method.addJavaDocLine(" */");

        return Collections.singletonList(method);
    }

    @Override
    public List<XmlElement> buildSQL(IntrospectedTable introspectedTable) {
        TextElement text1 = new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime());

        XmlElement trimColumn = new XmlElement("trim");
        trimColumn.addAttribute(new Attribute("prefix", "("));
        trimColumn.addAttribute(new Attribute("suffix", ")"));
        trimColumn.addAttribute(new Attribute("suffixOverrides", ","));

        XmlElement includeColumn = new XmlElement("include");
        includeColumn.addAttribute(new Attribute("refid", "Base_Column_List"));
        trimColumn.addElement(includeColumn);

        TextElement text2 = new TextElement("values");

        XmlElement foreach = new XmlElement("foreach");
        foreach.addAttribute(new Attribute("collection", "dataCollection"));
        foreach.addAttribute(new Attribute("item", "data"));
        foreach.addAttribute(new Attribute("separator", ","));

        XmlElement trimData = new XmlElement("trim");
        trimData.addAttribute(new Attribute("prefix", "("));
        trimData.addAttribute(new Attribute("suffix", ")"));
        trimData.addAttribute(new Attribute("suffixOverrides", ","));

        XmlElement includeProperty = new XmlElement("include");
        includeProperty.addAttribute(new Attribute("refid", "Insert_Property_List"));
        trimData.addElement(includeProperty);

        foreach.addElement(trimData);

        XmlElement insertBatch = new XmlElement("insert");
        insertBatch.addAttribute(new Attribute("id", "insertBatch"));

        insertBatch.addElement(text1);
        insertBatch.addElement(trimColumn);
        insertBatch.addElement(text2);
        insertBatch.addElement(foreach);

        return Collections.singletonList(insertBatch);
    }
}
